package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> result = new HashMap<>();
//        Integer inventoryTotal = goodsService.getInventoryTotalCount();
        PageInfo<Goods> goodsPageInfo = goodsService.listInventory(page, rows, codeOrName, goodsTypeId);
//        System.out.println("------------------");
//        System.out.println(goodsInventoryList.toString());
//        System.out.println("------------------");

        result.put("total", goodsPageInfo.getTotal());
        result.put("rows", goodsPageInfo.getList());
        return result;
    }


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> result = new HashMap<>();
        PageInfo<Goods> goodsPageInfo = goodsService.getGoodsList(page, rows, goodsName, goodsTypeId);
        result.put("total", goodsPageInfo.getTotal());
        result.put("rows", goodsPageInfo.getList());
        return result;
    }

    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */
    @RequestMapping("/save")
    public ServiceVO<Void> save(Goods goods) {
        /*System.out.println("--------------------");
        //Goods(goodsId=null, goodsCode=0028, goodsName=1, inventoryQuantity=null,
        // lastPurchasingPrice=0.0, minNum=1, goodsModel=1, goodsProducer=1,
        // purchasingPrice=1.0, remarks=1, sellingPrice=1.0, state=null,
        // goodsUnit=元, goodsTypeId=6, goodsTypeName=null, saleTotal=null)
        System.out.println(goods);
        System.out.println("--------------------");*/

        goodsService.save(goods);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("delete")
    public ServiceVO<Void> delete(Integer goodsId) {
        goodsService.delete(goodsId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        Map<String, Object> result = new HashMap<>();
        PageInfo<Goods> pageInfo = goodsService.getNoInventoryQuantity(page, rows, nameOrCode);
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;
    }

    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/getHasInventoryQuantity")
    public Map<String, Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        Map<String, Object> result = new HashMap<>();
        PageInfo<Goods> pageInfo = goodsService.getHasInventoryQuantity(page, rows, nameOrCode);
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;
    }


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("/saveStock")
    public ServiceVO<Void> saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice) {
        goodsService.saveStock(goodsId, inventoryQuantity, purchasingPrice);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("/deleteStock")
    public ServiceVO<Void> deleteStock(Integer goodsId) {
        goodsService.deleteStock(goodsId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("/listAlarm")
    public Map<String,Object> listAlarm() {
        Map<String,Object> result = new HashMap<>();
        List<Goods> goodsList = goodsService.listAlarm();
        result.put("rows", goodsList);
        return result;
    }

}
